//
//  RootViewController.m
//  03 为视频添加滤镜
//
//  Created by dllo on 16/3/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import "PlayViewController.h"

@interface RootViewController ()
// 显示进度
@property (strong, nonatomic) IBOutlet UILabel *progress;
// 显示
@property (strong, nonatomic) IBOutlet UIView *showView;
// 播放按钮
- (IBAction)play:(id)sender;

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    // 创建视图
    [self createView];
}
- (void)createView{
    
    // 指定要添加滤镜的视频的位置
    NSURL *sampleURL = [[NSBundle mainBundle] URLForResource:@"movie" withExtension:@"mov"];
    
    movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
    movieFile.runBenchmark = YES;
    movieFile.playAtActualSpeed = NO;
    // 创建滤镜
    filter = [[GPUImagePixellateFilter alloc] init];
    // 添加滤镜
    [movieFile addTarget:filter];
    
    // 创建显示视图
    GPUImageView *filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, 414, 414)];
    [self.showView addSubview:filterView];
    [filter addTarget:filterView];
    
    // 设置添加了滤镜的视频保存的位置
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mov"];
    // 如果有同名文件存在,就删除
    unlink([pathToMovie UTF8String]);
    
    NSLog(@"%@",pathToMovie);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
    // 指定输出文件的位置和尺寸
    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];
    [filter addTarget:movieWriter];
    
    movieWriter.shouldPassthroughAudio = YES;
    movieFile.audioEncodingTarget = movieWriter;
    [movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

    // 开始编辑视频
    [movieWriter startRecording];
    [movieFile startProcessing];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:0.3f
                                             target:self
                                           selector:@selector(retrievingProgress)
                                           userInfo:nil
                                            repeats:YES];
    // 当编辑结束的时候出发
    [movieWriter setCompletionBlock:^{
        // 移除,不然会一直占有大量的内存
        [filter removeTarget:movieWriter];
        [movieWriter finishRecording];
        //        movieWriter.
        dispatch_async(dispatch_get_main_queue(), ^{
            [timer invalidate];
            self.progress.text = @"100%";
        });
    }];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// 显示进度
- (void)retrievingProgress
{
    self.progress.text = [NSString stringWithFormat:@"%d%%", (int)(movieFile.progress * 100)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

// 跳转到播放页面
- (IBAction)play:(id)sender {
    
    PlayViewController *playVC = [[PlayViewController alloc] init];
    [self presentViewController:playVC animated:YES completion:^{
    }];
    
}
@end
